package com.budwk.app.waterquality.services.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysConfigService;
import com.budwk.app.utils.NutzIocUtils;
import com.budwk.app.waterquality.component.CommonComponent;
import com.budwk.app.waterquality.config.AppException;
import com.budwk.app.waterquality.models.FileInfo;
import com.budwk.app.waterquality.services.FileInfoService;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.starter.storage.StorageServer;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.upload.TempFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

@IocBean(args = {"refer:dao"})
@Slf4j
public class FileInfoServiceImpl extends BaseServiceImpl<FileInfo> implements FileInfoService {
    public FileInfoServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private StorageServer storageServer;
    @Inject
    private SysConfigService sysConfigService;

    @Override
    public Result<?> uploadFile(TempFile file) {
        // 获取文件后缀
        String suffixName = file.getSubmittedFileName().substring(file.getSubmittedFileName().lastIndexOf(".")).toLowerCase();
        // 文件存储全路径
        String fileAllPath = NutzIocUtils.getUploadPath() + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN) + File.separator;
        File file1 = new File(fileAllPath);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        // 文件名称
        String fileName = R.UU32() + suffixName;
        // 文件路径
        String filePath = fileAllPath;
        try {
            // 准备文件夹,获取项目中upload文件夹的路径
            NutMap map = storageServer.upload(file.getInputStream(), fileName, filePath);
            FileInfo f = new FileInfo();
            f.setPath(map.getString("url")).setUploadFileName(file.getSubmittedFileName());
            saveData(f);
            this.insert(f);
            return Result.data(f);
        } catch (IOException e) {
            log.error("upload error:", e);
            throw new AppException("文件服务异常");
        }
    }

    @Override
    public FileInfo saveData(FileInfo f) {
        Sys_user user = CommonComponent.getCurrentUser();
        f.setUploadBy(user.getId())
                .setUploadTime(LocalDateTime.now()).setUploadName(user.getUsername())
                .setCreateId(user.getId()).setCreateDate(LocalDateTime.now())
                .setUpdateId(user.getId()).setUpdateDate(LocalDateTime.now());

        Sys_unit unit = user.getUnit();
        f.setUnitId(unit != null ? unit.getId() : null);
        f.setUnitName(unit != null ? unit.getName() : null);
        return f;
    }
}
